<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\validate;
class Friends extends Controller{
    public function getIndex(){
        $request=request();
        $data=Db::table('tp_friends')->select();
        //var_dump($data);
        return $this->fetch("friends/index",['data'=>$data]);
    }
    //执行添加
    public function postindex(){
            $request=request();
            $data=$request->param();
            //var_dump($data);
            unset($data['action']);
        //把路径赋值给$path
        $path=$data['friends_path'];
        //判断前缀是否是http://和https://
        if ((substr($path, 0, 7) != 'http://') && (substr($path, 0, 8) != "https://")) {
            //如不是就拼接
            $path = "http://" .$path;
        }
        //把赋值的结果承储起来
        $data["friends_path"] = $path;
        //调用validate
        $result=$this->validate($request->param(),'Friends');
        if(true!==$result){
            $this->error($result) ;
        }
        //把数据承储到数据库里
        $info=Db::table("tp_friends")->insert($data);
        //var_dump($s) ;exit;
        if($info){
           $this->success("添加成功",'http://www.thinkphp5.com/friends/index');
       }else{
             $this->error("添加失败");
       }
    }
}

